<?php
namespace App\Http\Controllers\SystemManagement;

use App\Http\Controllers\Controller;
use App\Models\Constant\Errors;
use App\Models\Constant\Fields;
use App\Models\Error;
use App\Models\SystemManagement\Role;
use App\Models\SystemManagement\RolePrivilege;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * Class RolePrivilegeController
 * @package App\Http\Controllers\SystemManagement
 * @group 角色权限管理
 */
class RolePrivilegeController extends Controller{
    /**
     * 角色权限关系
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam id int required 角色id
     *
     * @queryParam id int 角色id
     * @queryParam name 角色名
     * @queryParam role_privilege 角色权限关系:{"id:1,"privilege":"1,2,3"}
     *
     * @response 200 {"code":200,"msg":"Success","data":[{"id":1,"mark":1,"name":"\u8d85\u7ea7\u7ba1\u7406\u5458","role_privilege":[{"id":1,"privilege":"1,2,3,4,5"},{"id":2,"privilege":"2"}]}]}
     * @response 404 {"code":404,"msg":"mot found","data":"没有查询到相关数据"}
     */
    public function getRolePri($id){
        $data = Role::with('rolePrivilege')
            ->where(Fields::ID,$id)
            ->get(['id','name']);
        $err = new Error();
        if($data->isEmpty()){
            $err->setError(Errors::NOT_FOUND);
            $err->setData('没有查询到相关数据');
        }else{
            $err->setData($data);
        }
        return response()->json($err,200);
    }

    /**
     * 角色授权
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam role_id int required 角色ID
     * @bodyParam privilege string required 权限ID：1,2,3
     *
     * @queryParam id int 角色权限关系id
     * @queryParam role_id int 角色ID 外键 角色表
     * @queryParam privilege string 权限id：1,2,3
     *
     * @response 201 {"code":201,"msg":"created","data":"成功"}
     * @response 400 {"code":400,"msg":"param error","data":"参数错误"}
     */
    public function addRolePri(\App\Http\Requests\System\RolePrivilege $request){
        $err = new Error();
        if(count($request->input())==0){
            $err->setError(Errors::FAILED);
            $err->setData('请添加数据');
            return response()->json($err);
        }
        try{
            $data = $request->only(['role_id','privilege']);
            $privilegeIds = explode(',', $data['privilege']);
            unset($data['privilege']);
            foreach ($privilegeIds as $key => $value) {
                $data['privilege_id'] = $value;
                $res = RolePrivilege::where($data)->first();
                if(empty($res)){
                    $rp = RolePrivilege::create($data);
                } else {
                    $rp = "权限已存在";
                }
            }
            $err->setError(Errors::COK);
            $err->setData($rp);
        }catch (\Exception $e){
            dd($e);
            $err->setError(Errors::FAILED);
            $err->setData('添加失败');
        }
        return response()->json($err);
    }

    /**
     * 角色权限修改
     * @param Request $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam role_id int required 角色ID
     * @bodyParam privilege string required 权限ID：1,2,3
     *
     * @response 205 {"code":205,"msg":"updated","data":"成功"}
     * @response 400 {"code":400,"msg":"param error","data":"参数错误"}
     */
    public function updateRolePri(\App\Http\Requests\System\RolePrivilege $request ,$id){
        $err = new Error();
        if(count($request->input()) ==0){
            $err->setError(Errors::FAILED);
            $err->setData('请添加数据');
            return response()->json($err);
        }
        DB::beginTransaction();
        try{
            $data = $request->input();
            $privilegeIds = explode(',', $data['privilege']);
            unset($data['privilege']);
            RolePrivilege::where('role_id',$data['role_id'])->delete();
            foreach ($privilegeIds as $key => $value) {
                $data['privilege_id'] = $value;
                RolePrivilege::create($data);
            }
            DB::commit();
            $err->setError(Errors::UOK);
            $err->setData("修改成功");
        }catch (\Exception $e){
            DB::rollBack();
            //dd($e->getMessage());
            $err->setError(Errors::FAILED);
            $err->setData('修改失败');
        }
        return response()->json($err);
    }
}
